layui.use(['element', 'form', 'table', 'layer', 'laydate'], function () {
    var layer = layui.layer;
    var $ = layui.$;
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;

    laydate.render({
        elem:'#dealTime'
    })
    laydate.render({
        elem:'#dealTime1'
    })

    //未结算表格数据
    table.render({
        elem: '#userTable',
        url: './selectSettleMent?status=0',
        page: true,
        height: 'full-50',
        minLength: 80,
        toolbar: '#userToolBar',
        cols: [
            [ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'dealTime', title: '交易日期', width: 150, align: 'center'}
                , {field: 'cashId', title: '现金id', width: 150, align: 'center', hide: true}
                , {field: 'dealId', title: '交易Id', width: 150, align: 'center', hide: true}
                , {field: 'dealNumber', title: '交易数量', width: 150, align: 'center'}
                , {field: 'dealPrice', title: '交易单价', width: 150, align: 'center'}
                , {field: 'endMoney', title: '结算金额', width: 150, align: 'center'}
                , {field: 'dealMoney', title: '交易金额', width: 150, align: 'center'}
                , {field: 'endTime', title: '结算日期', width: 150, align: 'center'}
                , {field: 'accountName', title: ' 现金账户', width: 210, align: 'center'}
                , {field: 'securityName', title: ' 证券名称', width: 150, align: 'center'}
                , {field: 'brokerName', title: '券商名称', width: 150, align: 'center'}
                , {
                field: 'dealWay', title: '交易方式', width: 150, align: 'center', templet: function (item) {
                    if (item.dealWay == '1') {
                        return '买入';
                    } else if (item.dealWay == '2') {
                        return '卖出';
                    } else if (item.dealWay == '3') {
                        return '分红';
                    } else if (item.dealWay == '4') {
                        return '送股';
                    }
                }
            }
                , {
                field: 'dealState', title: '交易状态', width: 150, align: 'center', templet: function (item) {
                    if (item.dealState == '0') {
                        return '未结算';
                    } else if (item.dealState == '1') {
                        return '已结算';
                    }
                }
            }
            ]
        ],
        //返回参数取名字
        request: {
            pageName: 'page',   // 页码的参数名称，默认：page
            limitName: 'row',  // 每页数据量的参数名，默认：limit
        }
    });


    //已结算表格数据
    table.render({
        elem: '#userTable2',
        url: './selectSettleMent?status=1',
        page: true,
        height: 'full-100',
        minLength: 80,
        toolbar: '#userToolBar2',
        cols: [
            [ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'dealTime', title: '交易日期', width: 150, align: 'center'}
                , {field: 'dealNumber', title: '交易数量', width: 150, align: 'center'}
                , {field: 'dealPrice', title: '交易单价', width: 150, align: 'center'}
                , {field: 'endMoney', title: '结算金额', width: 150, align: 'center'}
                , {field: 'dealMoney', title: '交易金额', width: 150, align: 'center'}
                , {field: 'endTime', title: '结算日期', width: 150, align: 'center'}
                , {field: 'accountName', title: ' 现金账户', width: 210, align: 'center'}
                , {field: 'securityName', title: ' 证券名称', width: 150, align: 'center'}
                , {field: 'brokerName', title: '券商名称', width: 150, align: 'center'}
                , {
                field: 'dealWay', title: '交易方式', width: 150, align: 'center', templet: function (item) {
                    if (item.dealWay == '1') {
                        return '买入';
                    } else if (item.dealWay == '2') {
                        return '卖出';
                    } else if (item.dealWay == '3') {
                        return '分红';
                    } else if (item.dealWay == '4') {
                        return '送股';
                    }
                }
            }
                , {
                field: 'dealState', title: '交易状态', width: 150, align: 'center', templet: function (item) {
                    if (item.dealState == '0') {
                        return '未结算';
                    } else if (item.dealState == '1') {
                        return '已结算';
                    }
                }
            }
            ]
        ],
        //返回参数取名字
        request: {
            pageName: 'page',   // 页码的参数名称，默认：page
            limitName: 'row',  // 每页数据量的参数名，默认：limit
        }
    });


    //给工具条的结算添加事件
    table.on('toolbar(userTable)', function (obj) {
        switch (obj.event) {
            case "search":
                //获得选中的对象
                var checkStatus = table.checkStatus("userTable"); //idTest 即为基础参数 id 对应的值
                // 其中 ID 为基础参数 id 对应的值（见：设定容器唯一ID）
                var dataArr = checkStatus.data; //获取选中行的数据
                var settlement = JSON.stringify(dataArr);
                if (dataArr.length == 0) {
                    layer.msg("请先选中数据");
                } else {
                    layer.confirm('真的要结算吗', function () {
                        //向服务端发送结算指令
                        $.post("./updateSettleMent", "myJson=" + settlement + "&&flag=false", function (msg) {
                            table.reload('userTable2');
                            table.reload('userTable');  /*渲染刷新*/
                            layer.closeAll();
                            if (msg > 0) {
                                layer.msg('结算成功!', {icon: 1, time: 1000, shade: [0.5, '#000', true]});
                            } else {
                                layer.msg('结算失败', {icon: 2, time: 1000, shade: [0.5, '#000', true]})
                            }
                        });
                        return false;
                    });
                }
                break;
            case 'select':
                var dealTime=$("#dealTime").val()
                var dealWay=$("#dealWay").val()
                //表格的重新加载事件
                table.reload('userTable', {
                    method: 'post'
                    , where: {
                        'dealTime': dealTime,
                        'dealWay': dealWay,
                    }
                    , page: {
                        curr: 1
                    }
                });
                $("#dealTime").val(dealTime);
                $("#dealWay").val(dealWay);
                laydate.render({
                    elem:'#dealTime'
                })

                break;
        }
    });
    //给工具条的反结算添加事件
    table.on('toolbar(userTable2)', function (obj) {
        var settlement = [];
        switch (obj.event) {
            case "search2":
                //获得选中的对象
                var checkStatus = table.checkStatus("userTable2"); //idTest 即为基础参数 id 对应的值
                // 其中 ID 为基础参数 id 对应的值（见：设定容器唯一ID）
                var dataArr = checkStatus.data; //获取选中行的数据
                var settlement = JSON.stringify(dataArr);
                if (dataArr.length == 0) {
                    layer.msg("请先选中数据");
                } else {
                    layer.confirm('真的要反结算嘛', function () {
                        layer.close();
                        //向服务端发送删除指令
                        $.post("./updateSettleMent", "myJson=" + settlement + "&&flag=true", function (msg) {
                            if (msg > 0) {
                                table.reload('userTable2');
                                table.reload('userTable');  /*渲染刷新*/
                                layer.closeAll();
                                layer.msg('反结算成功!', {icon: 1, time: 1000, shade: [0.5, '#000', true]});
                            } else {
                                layer.closeAll();
                                layer.msg('反结算失败!', {icon: 2, time: 1000, shade: [0.5, '#000', true]});
                            }
                        });
                        return false;
                    });
                }
                break;
            case 'select':
                var dealTime1=$("#dealTime1").val()
                var dealWay1=$("#dealWay1").val()
                //表格的重新加载事件
                table.reload('userTable2', {
                    method: 'post'
                    , where: {
                        'status': 1,
                        'dealTime': dealTime1,
                        'dealWay': dealWay1,
                    }
                    , page: {
                        curr: 1
                    }
                });
                $("#dealTime1").val(dealTime1);
                $("#dealWay1").val(dealWay1);
                laydate.render({
                    elem:'#dealTime1'
                })
                break;
        }
    });
});